package cn.bai.mvc;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * Created by Admin on 12/23 0023.
 */
@WebServlet(urlPatterns = "*.do")
public class CustomerServlet extends HttpServlet {
    public CustomerDao customerDao=new CustomerDaoImp();
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request,response);

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取 ServletPath ：
        String requestPath=request.getServletPath();
        System.out.println(requestPath);
        //去除/ 和 do，得到类似于 add、delete 这样的字符串；
        String methodName=requestPath.substring(6,requestPath.length()-3);

        try {
            //利用反射获取methodName 对应的方法；
            Method method = getClass().getDeclaredMethod(methodName,
                    HttpServletRequest.class,HttpServletResponse.class);
            //利用反射调用对应的方法；
            method.invoke(this,request,response);
        } catch (Exception e) {
            // e.printStackTrace();
            response.sendRedirect("/page/error.jsp");
        }
    }

    public void query(HttpServletRequest request,HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("query...");
        String name=request.getParameter("name");
        String address=request.getParameter("address");
        String phone=request.getParameter("phone");

        CriteriaCustomer criteriaCustomer=new CriteriaCustomer(name,address,phone);
        List<Customer> customers=customerDao.getForListWithCriteriaCustomer(criteriaCustomer);
        request.setAttribute("customers",customers);
        request.getRequestDispatcher("/page/list.jsp").forward(request,response);

    }

    public void addCustomer(HttpServletRequest request,HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("addCustomer...");
        String name=request.getParameter("name");
        String address=request.getParameter("address");
        String phone=request.getParameter("phone");

        long cont=customerDao.getCountWithName(name);
        if(name.isEmpty() || address.isEmpty() || phone.isEmpty()){
            request.getRequestDispatcher("/page/chongxin.jsp").forward(request,response);
            return;
        }
        if(cont >0){
            String message="您输入的用户名"+name+"已经被占用,请重新输入!";
            request.setAttribute("message",message);
            request.getRequestDispatcher("/page/newcustomer.jsp").forward(request,response);
            return;
        }

        Customer customer=new Customer(name,address,phone);
        if(customer!=null){
            customerDao.save(customer);
            response.sendRedirect("/page/success.jsp");
        }
    }

    public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException {
        System.out.println("delete...");
        String idStr=request.getParameter("id");
        int id=0;
        try{
            id=Integer.parseInt(idStr);
            customerDao.delete(id);

        }catch (Exception e){}
        response.sendRedirect("query.do");
    }


    public void edit(HttpServletRequest request,HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("edit...");
        String forwardPath="/page/error.jsp";
        String id=request.getParameter("id");
        try{
            Customer customer=customerDao.get(Integer.parseInt(id));
            if(customer !=null){
                forwardPath="/page/updatecustomer.jsp";
                request.setAttribute("customer",customer);
            }
        }catch (Exception e){

        }
        request.getRequestDispatcher(forwardPath).forward(request,response);
    }
    public void update(HttpServletRequest request,HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("update...");
        String id=request.getParameter("id");
        String name=request.getParameter("name");
        String address=request.getParameter("address");
        String phone=request.getParameter("phone");
        String oldName=request.getParameter("oldName");
        if( ! oldName.equalsIgnoreCase(name)){
            long count=customerDao.getCountWithName(name);
            if(count>0){
                request.setAttribute("message","用户名"+name+"已经被占用，请重新输入！");
                request.getRequestDispatcher("/page/updatecustomer.jsp").forward(request,response);
                return;
            }
        }
        Customer customer=new Customer(name,address,phone);
        customer.setId(Integer.parseInt(id));
        customerDao.update(customer);
       // response.sendRedirect("query.do");
        response.sendRedirect("/page/list.jsp");
    }


}
